package MainPackage;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.Dialog;
import java.lang.reflect.InvocationTargetException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author d.chauvistre
 */
public class Login extends javax.swing.JDialog {

    private Userlist userlist;
    private User eingeloggterUser = null;
    private boolean istEingeloggt=false;
    
    /**
     * Creates new form Login
     */
    public Login(Userlist userlist) {
        this.userlist = userlist;        
        initComponents();
        this.setModal(true);
        this.setVisible(true);
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    //@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        name = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        einloggen = new javax.swing.JToggleButton();
        registrieren = new javax.swing.JToggleButton();
        hilfe = new javax.swing.JToggleButton();
        passwort = new javax.swing.JPasswordField();

        setTitle("Login");
        setAlwaysOnTop(true);
        setModal(true);
        setResizable(false);

        jLabel1.setText("Name");

        jLabel2.setText("Passwort");

        einloggen.setText("Ok");
        einloggen.setToolTipText("einloggen");
        einloggen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                einloggenActionPerformed(evt);
            }
        });

        registrieren.setText("Registrieren");
        registrieren.setToolTipText("registrieren");
        registrieren.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registrierenActionPerformed(evt);
            }
        });

        hilfe.setText("Hilfe");
        hilfe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hilfeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(90, 90, 90)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(einloggen)
                                .addGap(18, 18, 18)
                                .addComponent(registrieren)
                                .addGap(27, 27, 27)
                                .addComponent(hilfe))
                            .addComponent(name, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(passwort, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addContainerGap(92, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(einloggen)
                    .addComponent(registrieren)
                    .addComponent(hilfe))
                .addContainerGap(73, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void einloggenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_einloggenActionPerformed
        User user = userlist.getUser(name.getText());
        if(user != null){
            String password = User.sha1(new String(this.passwort.getPassword()));            
            if(user.getPasswort().equals(password)){
                this.eingeloggterUser = user;
                this.setVisible(false);
            }else{
                JOptionPane.showMessageDialog(this,"Passwort falsch!","Error",JOptionPane.ERROR_MESSAGE);
            }
        }else{
            JOptionPane.showMessageDialog(this,"Loginname nicht bekannt","Error",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_einloggenActionPerformed

    private void registrierenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registrierenActionPerformed
        this.setVisible(false);    
        Registration.ShowRegistration(this.userlist);
        this.setVisible(true);
    }//GEN-LAST:event_registrierenActionPerformed

    private void hilfeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hilfeActionPerformed
            //TODO
    }//GEN-LAST:event_hilfeActionPerformed

    public static User getLoggedinUser(Userlist userlist){
        Login login = new Login(userlist);                
        return login.eingeloggterUser;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton einloggen;
    private javax.swing.JToggleButton hilfe;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField name;
    private javax.swing.JPasswordField passwort;
    private javax.swing.JToggleButton registrieren;
    // End of variables declaration//GEN-END:variables
}
